package com.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.staffDao;

/**
 * Servlet implementation class export
 */
@WebServlet("/export.xls")
public class export extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public export() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setContentType("text/application;charset=GBK");
		staffDao dao = new staffDao();
		String flag = request.getParameter("flag");
		List<Map> list;
		if(flag.equals("yes") || flag.equals("no") || flag.equals("outdate")) {
			if(!flag.equals("outdate")) {
				Map mn = new HashMap();
				mn.put("parm", flag);
				list = dao.isreturnpp(mn);
			}
			else list = dao.searchOutDate();
			String res = "";
			for(int i=0;i<list.size();i++) {
				Map m = list.get(i);
				res+=m.get("name")+"\t";
				res+=m.get("personid")+"\t";
				res+=m.get("sex")+"\t";
				res+=m.get("nation")+"\t";
				res+=m.get("face")+"\t";
				res+=m.get("department")+"\t";
				res+=m.get("education")+"\t";
				res+=m.get("contact")+"\t";
				res+=m.get("pid")+"\t";
				res+=m.get("kind")+"\t";
				res+=m.get("startdate")+"\t";
				res+=m.get("enddate")+"\t";
				res+=m.get("returndate")+"\t\n";
			}
			res = res.replaceAll("\\{", "");
			res = res.replaceAll("\\}", "");
			res = res.replaceAll("\r", "");
			System.out.println(res);
			response.getWriter().print(res);
			return;
		}
		
		String country = request.getParameter("country");
		String college = request.getParameter("college");
		String subject = request.getParameter("subject");
		String outdate = request.getParameter("outdate");
		String backdate = request.getParameter("backdate");
		String costby = request.getParameter("costby");
		String returndate = request.getParameter("returndate");
		String remake = request.getParameter("remake");
		
		String pid = request.getParameter("pid");
		String kind = request.getParameter("kind");
		String startdate = request.getParameter("startdate");
		String enddate = request.getParameter("enddate");
		
		String name = request.getParameter("name");
		String personid = request.getParameter("personid");
		String sex = request.getParameter("sex");
		String nation = request.getParameter("nation");
		String face = request.getParameter("face");
		String job = request.getParameter("job");
		String adl = request.getParameter("adl");
		String post = request.getParameter("post");
		String outtype = request.getParameter("outtype");
		String department = request.getParameter("department");
		String education = request.getParameter("education");
		String contact = request.getParameter("contact");
		
		Map mm = new HashMap();
		mm.put("country", country);
		mm.put("college", college);
		mm.put("subject", subject);
		mm.put("outdate", outdate);
		mm.put("backdate", backdate);
		mm.put("costby", costby);
		mm.put("returndate", returndate);
		mm.put("remake", remake);
		mm.put("pid", pid);
		mm.put("kind", kind);
		mm.put("startdate",startdate );
		mm.put("enddate", enddate);
		mm.put("name", name);
		mm.put("personid", personid);
		mm.put("sex", sex);
		mm.put("nation", nation);
		mm.put("face", face);
		mm.put("job",job );
		mm.put("adl",adl );
		mm.put("outtype",outtype );
		mm.put("post",post );
		mm.put("department", department);
		mm.put("education", education);
		mm.put("contact", contact);
		
		
		list = dao.search(mm);
		String res = "";
		for(int i=0;i<list.size();i++) {
			Map m = list.get(i);
			res+=m.get("name")+"\t";
			res+=m.get("personid")+"\t";
			res+=m.get("sex")+"\t";
			res+=m.get("nation")+"\t";
			res+=m.get("face")+"\t";
			res+=m.get("job")+"\t";
			res+=m.get("adl")+"\t";
			res+=m.get("department")+"\t";
			res+=m.get("education")+"\t";
			res+=m.get("contact")+"\t";
			res+=m.get("pid")+"\t";
			res+=m.get("kind")+"\t";
			res+=m.get("startdate")+"\t";
			res+=m.get("enddate")+"\t";
			res+=m.get("country")+"\t";
			res+=m.get("college")+"\t";
			res+=m.get("subject")+"\t";
			res+=m.get("outdate")+"\t";
			if((m.get("backdate")).equals("")) res+="未回\t";
			else res+=m.get("backdate")+"\t";
			res+=m.get("costby")+"\t";
			if((m.get("returndate")).equals("")) res+="尚未归还\t";
			else res+=m.get("returndate")+"\t";
			if((m.get("remake")).equals("")) res+="无\t\n";
			else res+=m.get("remake")+"\t\n";
		}
		res = res.replaceAll("\\{", "");
		res = res.replaceAll("\\}", "");
		res = res.replaceAll("\r", "");
		System.out.println(res);
		response.getWriter().print(res);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
